<?php

namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\model\stock\Stock;
use app\common\model\stock\StockYtdPrice;
use app\common\define\StockDefine;

/**
 * 获取收盘价（当天收盘后【下午五点跑脚本】，跑出当天的收盘价格，用于明天涨跌幅的计算）
 * Class StockYtdClosePrice
 * @package app\api\command
 */
class StockYtdClosePrice extends Command{

    protected function configure(){
        $this->setName('StockYtdClosePrice')->setDescription('获取收盘价');
    }

    protected function execute(Input $input, Output $output){
        $token = StockDefine::STOCK_API_TOKEN;

        //获取股票列表
        $field = 'name,code,type';
//        $stockList = collection(Stock::field($field)->select())->toArray();
        $stockList = collection(Stock::field($field)->where('type', 300)->select())->toArray();
        $date = date('Y-m-d');
        //$date = date('Y-m-d', strtotime('-3 days'));
        //calculationCycle周期：100-日，101-周,102-月
        $requestUrl = 'https://stockapi.com.cn/v1/base/day';
        foreach ($stockList as $item) {
            $code = $item['code'];
            $requestUri = "{$requestUrl}?token={$token}&code={$code}&startDate={$date}&endDate={$date}&calculationCycle=100";
            $ret = get_curl_data($requestUri);
            $result = $ret[1];
            $result = json_decode($result, true);
            if (isset($result['data'][0]) && !empty($result['data'][0])) {
                $data = $result['data'][0];
                /*判断是否满足第二天条件
                    价格（开盘价）  开收盘价格浮动范围
                    0元~10元      0元
                    10元~50元     0元~0.05元
                    50元~100元    0元~0.1元
                    100元~150元   0元~0.15元
                    150元~200元   0元~0.2元
                    200元以上      0元~0.3元
                满足以上条件的数据入库
                */
                $mark = false;
                $diff = $data['open'] - $data['close'];
                //0元~10元      0元
                if ($data['open'] <= 10 && $diff == 0) {
                    $mark = true;
                }
                //10元~50元     0元~0.05元
                if (($data['open'] > 10 && $data['open'] <= 50) && abs($diff) <= 0.05) {
                    $mark = true;
                }

                //50元~100元    0元~0.1元
                if (($data['open'] > 50 && $data['open'] <= 100) && abs($diff) <= 0.1) {
                    $mark = true;
                }

                //100元~150元   0元~0.15元
                if (($data['open'] > 100 && $data['open'] <= 150) && abs($diff) <= 0.15) {
                    $mark = true;
                }

                //150元~200元   0元~0.2元
                if (($data['open'] > 150 && $data['open'] <= 200) && abs($diff) <= 0.2) {
                    $mark = true;
                }

                //200元以上      0元~0.3元
                if ($data['open'] > 200 && abs($diff) <= 0.3) {
                    $mark = true;
                }

                if (!$mark) {
                    continue;
                }
                $insert = [
                    'name' => $item['name'],
                    'code' => $code,
                    'type' => $item['type'],
                    'open_price' => $data['open'],
                    'close_price' => $data['close'],
                    'change_ratio' => $data['changeRatio'],
                    'date' => $data['time'],
                    'create_time' => time(),
                ];
                $re = StockYtdPrice::insert($insert);
                echo $item['name'].$item['code'].'处理结果为：'.$re."\n\r";
            }
//            sleep(1);
        }
    }
}
